package com.ruoyi.ai.controller;

import com.ruoyi.ai.dto.ChatRequestDTO;
import com.ruoyi.ai.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import javax.validation.Valid;

/**
 * @author 15332
 * @version 1.0
 * @date 2025/9/23 16:41
 * @description TODO
 */
@RestController
public class ChatController {

  /*@Autowired
  private OpenAiChatModel chatModel;*/

  @Autowired
  private ChatService chatService;

/*  @GetMapping("/ai/generate")
  public Map generate(@RequestParam(value = "message",defaultValue = "Tell me a joke") String message){
    return Map.of("generation",this.chatModel.call(message));
  }

  @GetMapping("/chat/stream")
  public Flux<ServerSentEvent<String>> chatStream(@RequestParam(value = "message",
    defaultValue = "Tell me a joke") String message){
    Prompt prompt = new Prompt(new UserMessage(message));
    return chatModel.stream(prompt)
      .map(response -> {
        String text = response.getResults().get(0).getOutput().getText();
        return ServerSentEvent.<String>builder()
          .event("chat")
          .data(text)
          .build();
      });
  }*/

  @GetMapping("/chat/stream")
  public Flux<ServerSentEvent<String>> chat(
    @Valid ChatRequestDTO requestDTO
    ){
    return chatService.chatWithTools(requestDTO).map(response -> {
      String text = response.getResults().get(0).getOutput().getText();
      return ServerSentEvent.<String>builder()
        .event("chat")
        .data(text)
        .build();
      });
  }
}
